package cn.gailvlun.gll.widget

import android.graphics.Canvas
import android.graphics.Paint
import android.support.annotation.ColorInt
import android.support.v7.widget.RecyclerView

class CommonItemDecoration: RecyclerView.ItemDecoration {
    
    constructor(leftOffset: Int, rightOffset: Int, strokeWidth: Int,
                @ColorInt decorColor: Int, @ColorInt bgColor: Int, isLastDraw: Boolean = false) : super() {
        this.leftOffset = leftOffset
        this.rightOffset = rightOffset
        this.strokeWidth = strokeWidth
        this.decorColor = decorColor
        this.bgColor = bgColor
        this.isLastDraw = isLastDraw

        mPaint.style = Paint.Style.FILL
        mPaint.strokeWidth = strokeWidth.toFloat()
        mPaint.isAntiAlias = true
    }

    private var leftOffset: Int
    private var rightOffset: Int
    private var strokeWidth: Int
    @ColorInt var decorColor: Int
    @ColorInt var bgColor: Int
    private var isLastDraw: Boolean
    private val mPaint = Paint()

    override fun onDrawOver(c: Canvas?, parent: RecyclerView, state: RecyclerView.State?) {
        super.onDrawOver(c, parent, state)

        val childCount = parent.childCount

        for (i in 0 until
                if (isLastDraw)
                    childCount
                else
                    childCount - 1) {
            val childView = parent.getChildAt(i)
            var params = childView.layoutParams as RecyclerView.LayoutParams

            val top: Float = (childView.bottom + params.bottomMargin).toFloat()
            val bottom: Float = (top + strokeWidth)
            val measuredWidth = parent.measuredWidth.toFloat()

            mPaint.color = bgColor
            c?.drawRect(0f, top, leftOffset.toFloat(), bottom, mPaint)
            mPaint.color = decorColor
            c?.drawRect(leftOffset.toFloat(), top, measuredWidth - rightOffset.toFloat(), bottom, mPaint)
            mPaint.color = bgColor
            c?.drawRect(measuredWidth - rightOffset.toFloat(), top, measuredWidth, bottom, mPaint)
        }
    }
}